<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/2/9
 * Time: 12:23
 */

namespace app\index\controller;

use think\Db;
use think\Controller;

class Api extends Controller
{
    //查询是否有待办任务，并发出提醒
    public function index(){
        //查询所有用户
        $users = db('users')->field('id')->select();
        foreach($users as $k=>$v){
            $sql = "select count(*)  cnt from  (
select distinct a.title as title, a.sendtime as sendtime 
from TW_HZ_WorkList a  
where (a.auth_id = '".$v['id']."' OR a.agent_id = '".$v['id']."') AND isactive = '1' AND status_no < '200' 
order by a.sendtime desc ) t ";
            $rs = Db::connect('db_config1')->query($sql);
            $v['cnt'] = isset($rs[0]['cnt'])?$rs[0]['cnt']:0;
            $users[$k] = $v;
        }

        //逐个发送提醒
        foreach($users as $k=>$v){
            //判断用户个人的设置是否允许定点发送通知
            $eid = db('users')->where('id',$v['id'])->value('employee_id');
            $mttx = db('cg_grsz')->where('eid',$eid)->value('mttx');
            if($v['cnt']>0&&$mttx==1){
                $data = [];
                $data['title'] = ' 销路通提醒：您有未处理的待办流程 ';
                $data['fssj'] = date('Y-m-d H:i:s');
                $data['txrq'] = date('Y-m-d H:i:s');
                $data['txry'] = serialize([''.$v['id']]);
                $url = url('grzx/index');
                $data['content'] = <<<EOF
提醒： 销路通提醒：您有未处理的待办流程<br>
内容： 尊敬的张晓明，您好！<br>
您还有 {$v['cnt']} 个待办流程未处理。为不耽误相关工作开展，请尽快处理。谢谢您的配合。<br>
如果您不愿再收到此类提醒信息，请点击<a href="{$url}" class="J_menuItem">这里</a>设置。<br>
提醒时间： {$data['txrq']}<br>
<br>
<br>
消息提醒来源：销路通_快速消费品营销管理平台<br>
EOF;
                db('cg_message')->insert($data);
            }
        }
//        dump($users);
    }
}